'''
author: zpj

This file contains methods for generating sensor commands issued to sensors

'''

from crc16 import pack_data, crc16_cal

class SensorCommandGenerator:

    def get_function_object(self, function_name: str):
        '''输入函数名称返回对应函数'''
        return getattr(self, function_name, None)

    def set_sensor_light(self, value: int, sn: int=1) -> bytearray: # 设置传感器指示灯命令

        command = bytearray([0x06, sn, 0x07, 0x12, value]) 

        return pack_data(command)
    
    def set_alarm_threshold(self, hi_value: int, low_value: int, sn: int=1) -> bytearray: # 设置报警阈值

        command = bytearray([0x06, sn, 0x08, 0x11, hi_value, low_value])

        return pack_data(command)
    
    def read_alarm_threshold(self, sn: int=1) -> bytearray: # 读取报警阈值

        command = bytearray([0x03, sn, 0x06, 0x11])

        return pack_data(command)
    
    def set_acquisition_cycle(self, value: int, sn: int=1) -> bytearray: # 设置采集周期

        command = bytearray([0x06, sn, 0x07, 0x10, value])

        return pack_data(command)
    
    def read_acquisition_cycle(self, sn: int=1) -> bytearray: # 读取采集周期

        command = bytearray([0x03, sn, 0x06, 0x10])

        return pack_data(command)
    
    def set_fluctuation_threshold(self, value: int, sn: int=1) -> bytearray: # 设置波动阈值

        command = bytearray([0x06, sn, 0x07, 0x0F, value])

        return pack_data(command)
    
    def read_fluctuation_threshold(self, sn: int=1) -> bytearray: # 读取波动阈值

        command = bytearray([0x03, sn, 0x06, 0x0F])

        return pack_data(command)
    
    def set_channel(self, value: int, sn: int=1) -> bytearray: # 设置信道

        command = bytearray([0x06, sn, 0x07, 0x0A, value])

        return pack_data(command)
    
    def read_channel(self, sn: int=1) -> bytearray: # 读取信道

        command = bytearray([0x03, sn, 0x06, 0x0A])

        return pack_data(command)
    
    def set_reporting_cycle(self, hi_value: int, low_value: int, sn: int=1) -> bytearray: # 设置上报周期

        command = bytearray([0x06, sn, 0x08, 0x0C, hi_value, low_value])

        return pack_data(command)
    
    def read_reporting_cycle(self, sn: int=1) -> bytearray: # 读取上报周期

        command = bytearray([0x03, sn, 0x06, 0x0C])

        return pack_data(command)
    
    def set_zero_point(self, value: int, sn: int=1) -> bytearray: # 设置零点

        command = bytearray([0x06, sn, 0x07, 0x0B, value])

        return pack_data(command)
    
    def read_battery_level(self, sn: int=1) -> bytearray: # 读取电量

        command = bytearray([0x03, sn, 0x06, 0x06])

        return pack_data(command)
    
    def read_device_status(self, sn: int=1) -> bytearray: # 读取设备状态

        command = bytearray([0x03, sn, 0x06, 0x05])

        return pack_data(command)
    
    def read_firmware_version(self, sn: int=1) -> bytearray: # 读取固件版本

        command = bytearray([0x03, sn, 0x06, 0x03])

        return pack_data(command)
    
    def read_hardware_version(self, sn: int=1) -> bytearray: # 读取硬件版本

        command = bytearray([0x03, sn, 0x06, 0x02])

        return pack_data(command)
    
    def read_device_type(self, sn: int=1) -> bytearray: # 读取设备类型

        command = bytearray([0x03, sn, 0x06, 0x01])

        return pack_data(command)
    
    def read_signal_strength(self, sn: int=1) -> bytearray: # 读取信号强度

        command = bytearray([0x03, sn, 0x06, 0x07])

        return pack_data(command)
    
    def read_dip_angle(self, sn: int=1) -> bytearray: # 读取倾角

        command = bytearray([0x03, sn, 0x06, 0x0E])

        return pack_data(command)
    
    def read_pressure_value(self, sn: int=1) -> bytearray: # 读取压力
        '''读取压力'''

        command = bytearray([0x03, sn, 0x06, 0x13])

        return pack_data(command)


class ParseReturnValue:
    '''解释返回值'''

    def  parse_sensor_command(self, return_command):

        '''
        return_command: 03010706100610

        bytearray(b'\x01\x06\x00\x12\x00\x01\xe8\x0f')
        '''
        return_command_crc = return_command[-4:]

        return_command = return_command[:-4]

        bytearray_command_data = bytearray.fromhex(return_command)

        bytearray_command_crc_data = "0x" + return_command_crc

        if bytearray_command_crc_data.upper() != crc16_cal(bytearray_command_data).upper():

            return {
                'status': -1,
                'errorCode': 1,
                'errorMsg': "CRC验证失败",
                'resultBody': None
            }

        
        return {

            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': {
                "fun": return_command[0:2],
                "sn": return_command[2:4],
                "len": return_command[4:6],
                "addr": return_command[6:8],
                "value": return_command[8:]
            }
        }
    

if __name__ == "__main__":

    # c = SensorCommandGenerator()

    # v = c.read_pressure_value()

    # v = c.parse_command_to_ascII(v)

    # print(v)

    # print()

    p = ParseReturnValue()

    v = p.parse_sensor_command("0301070610d251")

    print(v)


    